/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator.magicindicator;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * UIUtil
 *
 * @since 2020-09-29
 */
public final class UiUtil {
    private static final float NUM2D0 = 2.0f;
    private static final int NUM2 = 2;

    private UiUtil() {
    }

    /**
     * vp2px
     *
     * @param context context
     * @param vpValue vpValue
     * @return px
     */
    public static int vp2px(Context context, float vpValue) {
        return AttrHelper.vp2px(vpValue, context);
    }

    /**
     * fp2px
     *
     * @param context context
     * @param fpValue fpValue
     * @return px
     */
    public static int fp2px(Context context, float fpValue) {
        return AttrHelper.fp2px(fpValue, context);
    }

    /**
     * getScreenWidth
     *
     * @param context context
     * @return ScreenWidth
     */
    public static int getScreenWidth(Context context) {
        Point point = new Point();
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        display.getSize(point);
        return point.getPointXToInt();
    }

    /**
     * getInterpolation
     *
     * @param curveType curveType
     * @param input input
     * @return Interpolation
     */
    public static float getInterpolation(int curveType, float input) {
        float result = input;
        switch (curveType) {
            case Animator.CurveType.LINEAR:
                result = input;
                break;
            case Animator.CurveType.ACCELERATE:
                result = (float) Math.pow(input, NUM2);
                break;
            case Animator.CurveType.DECELERATE:
                result = (float) (1.0f - Math.pow(1.0f - input, NUM2 * NUM2));
                break;
            default:
                break;
        }
        return result;
    }
}